package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.ProcessCheck;
import com.ruoyi.system.domain.bo.ProcessCheckBo;
import com.ruoyi.system.domain.vo.ProcessCheckVo;
import com.ruoyi.system.mapper.ProcessCheckMapper;
import com.ruoyi.system.service.IProcessCheckHistoryinfoService;
import com.ruoyi.system.service.IProcessCheckService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 流程审核Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-10
 */
@RequiredArgsConstructor
@Service
public class ProcessCheckServiceImpl implements IProcessCheckService {

    @Resource
    ProcessCheckMapper baseMapper;

    @Resource
     IProcessCheckHistoryinfoService iProcessCheckHistoryinfoService;

    @Override
    public Boolean successProcessCheck(Long id) {
        ProcessCheck processCheck = baseMapper.selectProcessCheckById(id);
        processCheck.setState("1");
        return baseMapper.updateProcessCheck(processCheck) == 1 ;
    }

    /**
     * 审核通过
     *
     * @param id
     * @param state
     * @return
     */
    @Override
    public Boolean editProcessCheckState(Long id, String state) {
        ProcessCheck processCheck = baseMapper.selectProcessCheckById(id);
        processCheck.setState(state);
        return baseMapper.updateProcessCheck(processCheck) == 1 ;
    }

    @Override
    public Boolean failProcessCheck(Long id) {
        ProcessCheck processCheck = baseMapper.selectProcessCheckById(id);
        processCheck.setState("0");
        return baseMapper.updateProcessCheck(processCheck) == 1 ;

    }

    /**
     * 查询流程审核
     */
    @Override
    public ProcessCheckVo queryById(Long id){

        ProcessCheck processCheck = baseMapper.selectProcessCheckById(id);
        return ProcessCheckVo.build(processCheck);
    }

    /**
     * 查询流程审核列表
     */
    @Override
    public TableDataInfo queryPageList(ProcessCheckBo bo) {
        return null;
    }

    /**
     * 查询流程审核列表
     */
    @Override
    public List<ProcessCheckVo> queryList(ProcessCheckBo bo) {
        return null;
    }



    /**
     * 新增流程审核
     * todo 联合事务
     */
    @Override
    public Boolean insertByBo(ProcessCheckBo bo) {
        ProcessCheck add = new ProcessCheck();
        BeanUtils.copyProperties(bo, add,"copyFor");
        add.setCopyFor(StringUtils.join(bo.getCopyFor(),","));
        add.setState("2");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insertProcessCheck(add) >0;
        if (flag) {
            bo.setId(add.getId());
            //创建审核历史记录
            iProcessCheckHistoryinfoService.createProcessCheckHistoryinfo(bo);
        }
        return flag;
    }





    /**
     * 修改流程审核
     */
    @Override
    public Boolean updateByBo(ProcessCheckBo bo) {
        ProcessCheck update = new ProcessCheck();
         BeanUtils.copyProperties(bo, update);
        validEntityBeforeSave(update);
        return baseMapper.updateProcessCheck(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProcessCheck entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除流程审核
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
       // return baseMapper.deleteBatchIds(ids) > 0;
        return null;
    }
}
